package com.sensor;

import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Formatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UDPPacket {
	byte[] device_type = new byte[]{(byte) 0xaa};
	byte[] command_type = new byte[]{(byte)0xaa};
	byte[] ip = new byte[4];
	byte[] mac = new byte[6];
	
	byte[] mask = new byte[4];
	byte[] gateway = new byte[4];
	byte[] broadcast = new byte[6];
	byte[] temperature = new byte[6];
	byte[] humiduty = new byte[6];
	byte[] dew_point = new byte[6];
	
	//byte[] id = new byte[2];
	byte[] AD_1 = new byte[2];
	byte[] AD_2 = new byte[2];
	byte[] AD_3 = new byte[2];
	byte[] AD_4 = new byte[2];
	byte[] ACK = new byte[1];
	
	public boolean FromByteStream(byte[] buf){
		if (buf.length < 55){
			System.out.print("len not enough: " + buf.length + "!\n");
			return false;
		}
		ByteBuffer buf_recv = ByteBuffer.wrap(buf);
		buf_recv.get(device_type, 0, device_type.length);
		buf_recv.get(command_type, 0, command_type.length);
		buf_recv.get(ip, 0, ip.length);
		buf_recv.get(mac, 0, mac.length);
		buf_recv.get(mask, 0, mask.length);
		buf_recv.get(gateway, 0, gateway.length);
		buf_recv.get(broadcast, 0, broadcast.length);
		buf_recv.get(temperature, 0, temperature.length);
		buf_recv.get(humiduty, 0, humiduty.length);
		buf_recv.get(dew_point, 0, dew_point.length);
		//buf_recv.get(id, 0, id.length);
		buf_recv.get(AD_1, 0, AD_1.length);
		buf_recv.get(AD_2, 0, AD_2.length);
		buf_recv.get(AD_3, 0, AD_3.length);
		buf_recv.get(AD_4, 0, AD_4.length);
		buf_recv.get(ACK, 0, ACK.length);
		
		return true;
	}
	
	public static String bytesToHexString(byte[] bytes) {  
	    StringBuilder sb = new StringBuilder(bytes.length * 2);  
	  
	    Formatter formatter = new Formatter(sb);  
	    for (byte b : bytes) {  
	        formatter.format("%02x", b);  
	    }  
	    return sb.toString();  
	}  
	
	public String GetIP() throws UnknownHostException{		
		String retval = InetAddress.getByAddress(ip).toString();
		return retval;
	}
	
	public String GetTemperature() throws UnsupportedEncodingException{
		String regEx = "(^\\d*\\.\\d*)";   
		Pattern pat = Pattern.compile(regEx);  
		
		String t_a = new String(temperature, "UTF-8");
		Matcher mat = pat.matcher(t_a);  
		boolean rs = mat.find();
		if (rs == true){
			return mat.group(0);
		} else {
			return null;			
		}
	}
	
	public String GetHumiduty() throws UnsupportedEncodingException{
		String regEx = "(^\\d*\\.\\d*)";   
		Pattern pat = Pattern.compile(regEx);  
		
		String t_a = new String(humiduty, "UTF-8");
		Matcher mat = pat.matcher(t_a);  
		boolean rs = mat.find();
		if (rs == true){
			return mat.group(0);
		} else {
			return null;			
		}
	}
	public String GetDewPoint() throws UnsupportedEncodingException{
		String regEx = "(^\\d*\\.\\d*)";   
		Pattern pat = Pattern.compile(regEx);  
		
		String t_a = new String(dew_point, "UTF-8");
		Matcher mat = pat.matcher(t_a);  
		boolean rs = mat.find();
		if (rs == true){
			return mat.group(0);
		} else {
			return null;			
		}
	}
	public String GetMac(){
		String retval = bytesToHexString(mac);
		return retval;
	}
	public String GetGateway() throws UnknownHostException{
		String retval = InetAddress.getByAddress(gateway).toString();
		return retval;
	}
	
	public String GetNetMask() throws UnknownHostException{
		String retval = InetAddress.getByAddress(mask).toString();
		return retval;
	}
	
	public String GetBroadcast(){
		String retval = bytesToHexString(broadcast);
		return retval;
	}
	public String GetAD1(){
		String retval = bytesToHexString(AD_1);
		return retval;
	}
	public String GetAD2(){
		String retval = bytesToHexString(AD_2);
		return retval;
	}
	public String GetAD3(){
		String retval = bytesToHexString(AD_3);
		return retval;
	}
	public String GetAD4(){
		String retval = bytesToHexString(AD_4);
		return retval;
	}
	public String GetACK(){
		String retval = bytesToHexString(ACK);
		return retval;
	}
	public String GetDeviceType(){
		String retval = bytesToHexString(device_type);
		return retval;
	}
	public String GetCommandType(){
		String retval = bytesToHexString(command_type);
		return retval;
	}
	@Override
	public String toString(){
		String retval;
		String t = null;
		String h = null;
		String d = null;
		try {
			// ����ƥ������Ҫ��ȡ����е�����"3.12"ԭʼ�����"3.12 0"
			String regEx = "(^\\d*\\.\\d*)";   
			Pattern pat = Pattern.compile(regEx);  
			
			String t_a = new String(temperature, "UTF-8");
			Matcher mat = pat.matcher(t_a);  
			boolean rs = mat.find();
			if (rs == true){
				t = mat.group(0);
			}
			
			String h_a = new String(humiduty, "UTF-8");
			mat = pat.matcher(h_a);
			rs = mat.find();
			if (rs == true){
				h = mat.group(0);
			}
			
			String d_a = new String(temperature, "UTF-8");
			mat = pat.matcher(d_a);
			rs = mat.find();
			if (rs == true){
				d = mat.group(0);
			}
			
//			System.out.print("haha..." + ma);
			retval = "(device_type: 0x" + bytesToHexString(device_type)
					+ ", command_type: 0x" + bytesToHexString(command_type)
					+ ", ip:" + InetAddress.getByAddress(ip).toString()
					+ ", mac:" + bytesToHexString(mac)
					+ ", mask:" + InetAddress.getByAddress(mask).toString()
					+ ", gateway:" + InetAddress.getByAddress(gateway).toString()
					+ ", broadcast:" + bytesToHexString(broadcast)
					+ ", temperature:" + mat.group(0) +"\u2103"
					+ ", humiduty:" + h + "%"
					+ ", dew_point:" + d + "\u2103"
					+ ", AD_1:" + bytesToHexString(AD_1)
					+ ", AD_2:" + bytesToHexString(AD_2)
					+ ", AD_3:" + bytesToHexString(AD_3)
					+ ", AD_4:" + bytesToHexString(AD_4)
					+ ", ACK:" + bytesToHexString(ACK)
					+ ")\n";

			return retval;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
 	
}
